/**
 * @Created by pangzg pangzg2@yusys.com.cn on 2020-4-13 13:56:49.
 * @updated by
 * @description 联系人管理
 */
define(function (require, exports) {
	/**
	 * 页面加载完成时触发
	 * @param hashCode 路由ID
	 * @param data 传递数据对象
	 * @param cite 页面站点信息
	 */
	exports.ready = function (hashCode, data, cite) {
		yufp.lookup.reg('USE_ALERTM_MODE');
		yufp.custom.vue({
			el: cite.el,
			data: function () {

				/*定义联系人名称校验*/
				var num = function (rule, value, callback) {
					var reg = /^[0-9]*$/;
					if (reg.test(value)) {
						callback(new Error('联系人名称不得为纯数字'));
					} else {
						callback();
					}
				};

				/*定义手机号码校验规则*/
				var phone = function (rule, value, callback) {
					var reg = /^1[3456789]\d{9}$/;
					if (reg.test(value)) {
						callback();
					} else {
						callback(new Error('手机号不合法，请检查'));
					}
				};

				/*定义校验规则，只能填字母数字 下划线*/
				var valiCodes = function (rule, value, callback) {
					var reg = /^[0-9a-zA-Z_]{1,}$/;
					if (value && reg.test(value)) {
						if (value.length > 64) {
							callback(new Error('长度不能大于64位'));
						} else {
							callback();
						}
					} else if (value && !reg.test(value)) {
						callback(new Error('能填字母、数字和下划线'));
					} else {
						callback();
					}
				};

				return {
					tbHeight:'0',
					rulesone: [{
						required: true,
						message: '必填项'
					},
					{
						validator: valiCodes,
						trigger: 'blur'
					}
					],
					mobileRule: [
						{
							required: true,
							message: '必填项'
						},
						{
							validator: phone,
							trigger: 'blur'
						}
					],
					nameRule: [
						{
							required: true,
							message: '必填项'
						},
						{
							validator: num,
							trigger: 'blur'
						}
					],

					dataUrl: 'api/citiccard/use/warning/contact/list?type=PERSON',
					relUrl: 'api/citiccard/use/warning/contact/detailsList?',

					addFnButton: !yufp.session.checkCtrl('addFn'),
					updateFnButton: !yufp.session.checkCtrl('modifyFn'),
					deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
					relFnButton: !yufp.session.checkCtrl('relFn'),

					formdata: {},
					addRelformdata: {},
					dialogVisible: false,
					formDisabled: false,
					saveDisabled: false,
					cancelBtnShow: true,
					relDialogVisible: false,
					addRelDialogVisible: false,
					viewTitle: "",
					placeholderGroup: '',
					addFlag: true,
					groupDisabled: true,
					searchFormdata: {},
					addSearchFormdata: {},
					options: []
				};
			},
			mounted:function(){
				var _this = this;
				//测试
				_this.$nextTick(function(){
						var tableBox = _this.$refs.tableBox;
						let a = tableBox.getBoundingClientRect().top + 28;
						var theight = `calc(100vh - ${a}px - 64px)`;
						_this.tbHeight = theight;		
				});
			},
			methods: {
				/**
				 * 所属组新增保存
				 */
				saveRelFn: function () {
					var _this = this;

					var model = {};
					var row = _this.$refs.refTable.selections[0];
					model.newRolename = row.rolename;//组员名
					model.rolename = _this.addRelformdata.rolename;//组名

					// 向后台发送保存请求
					yufp.service.request({
						method: 'POST',
						url: 'api/citiccard/use/warning/contact/addmembertogroup',
						data: model,
						callback: function (code, message, response) {
							if (response.code == 0) {

								// _this.$refs.refRelTable.remoteData();
								var param = {
									condition: JSON.stringify(_this.searchFormdata)
								};
								_this.$refs.refRelTable.remoteData(param);
								_this.$message('操作成功');
								_this.addRelDialogVisible = false;
							} else {
								_this.$message({ message: response.message, type: 'error', showClose: true, duration: 0 });
							}
						}
					});
				},
				/**
				 * 所属组删除保存
				 */
				deleteDelFn: function () {
					var _this = this;
					var selections = _this.$refs.refRelTable.selections;
					if (selections.length != 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
						return;
					}
					var model = {};
					yufp.clone(selections[0], model);
					// model.editeType = 'deleteDetails';//删除所属组标识
					model.newRolename = _this.$refs.refTable.selections[0].rolename;//组员名

					_this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning', duration: 6000,
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'POST',
									url: 'api/citiccard/use/warning/contact/deleteMemberFromGroup',//删除所属组关联数据
									data: model,
									callback: function (code, message, response) {
										if (response.code == 0) {
											var param = {
												condition: JSON.stringify(_this.searchFormdata)
											};
											_this.$refs.refRelTable.remoteData(param);
											_this.$message('操作成功');
										} else {
											_this.$message({ message: response.message, type: 'error', showClose: true, duration: 0 });
										}
									}
								});
							}
						}
					});
				},
				/**
				 * 所属组新增
				 */
				addRelFn: function () {

					var _this = this;
					yufp.service.request({
						method: 'GET',
						url: 'api/citiccard/use/warning/contact/getRolenames?nodeType=null',
						callback: function (code, message, response) {
							if (response.code == 0) {
								_this.options = [];
								var list = response.data;
								for (let index = 0; index < list.length; index++) {
									const node = list[index];
									_this.options.push({
										key: node.label,
										value: node.label
									});
								}
								_this.addRelDialogVisible = true;
								_this.$nextTick(function () {
									_this.$refs.addRelForm.resetFields();
								});
							} else {
								_this.$message({ message: "获取联系人组失败！", type: 'error', showClose: true, duration: 0 });
							}
						}
					});
				},
				/**
				 * 所属组查看
				 */
				relFn: function () {
					var _this = this;
					var selectionsAry = _this.$refs.refTable.selections;
					if (selectionsAry.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					_this.relDialogVisible = true;
					_this.relUrl = "api/citiccard/use/warning/contact/detailsList?";
					_this.relUrl = _this.relUrl + "rolename=" + selectionsAry[0].rolename + "&contact=" + selectionsAry[0].contact + "&nodeType=GROUP";
					this.$nextTick(function () {
						// _this.searchFormdata.rolename = selectionsAry[0].rolename;
						_this.searchFormdata.rolename = '';
						var param = {
							condition: JSON.stringify(_this.searchFormdata),
						};
						_this.$refs.refRelTable.remoteData(param);
						// _this.$refs.refRelTable.remoteData();
					});
				},
				/**
				 * 单击选择
				 */
				cellClick: function (row, event, column, index) {
					var _this = this;
					if (row) {
						_this.$refs.refTable.toggleRowSelection(row);
					}
				},
				/**
				 * 取消新增联系人页面
				 */
				cancelFn: function () {
					var _this = this;
					_this.dialogVisible = false;
				},
				/**
				 * 保存
				 */
				saveFn: function () {
					var _this = this;
					var model = {};
					yufp.clone(_this.formdata, model);
					var validate = false;
					var url = "";
					if (_this.addFlag) {
						url = "api/citiccard/use/warning/contact/save";//新增
					} else {
						url = "api/citiccard/use/warning/contact/update";//修改
					}
					_this.$refs.refForm.validate(function (valid) {
						validate = valid;
					});
					if (!validate) {
						return;
					}
					// 向后台发送保存请求
					yufp.service.request({
						method: 'POST',
						url: url,
						data: model,
						callback: function (code, message, response) {
							if (response.code == 0) {
								_this.$refs.refTable.remoteData();
								_this.$message('操作成功');
								_this.dialogVisible = false;
							} else {
								_this.$message({
									message: response.message,
									type: 'error', showClose: true, duration: 0
								});
							}
						}
					});
				},
				/**
				 * 控制保存按钮、xdialog、表单的状态
				 * @param viewType 表单类型
				 * @param editable 可编辑,默认false
				 */
				switchStatus: function (viewType, editable) {
					var _this = this;
					_this.viewType = viewType;
					_this.saveBtnShow = editable;
					_this.dialogVisible = true;
					_this.formDisabled = !editable;
					_this.saveDisabled = !editable;
				},
				/**
				 * 新增按钮
				 */
				addFn: function () {
					var _this = this;
					_this.addFlag = true;
					_this.viewTitle = "联系人组新增";
					_this.switchStatus('ADD', true);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
					});
				},
				/**
				 * 修改
				 */
				modifyFn: function () {
					var _this = this;
					_this.addFlag = false;
					_this.viewTitle = "联系人组修改";
					if (_this.$refs.refTable.selections.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					_this.switchStatus('UPDATE', true);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						var obj = _this.$refs.refTable.selections[0];
						yufp.clone(obj, _this.formdata);
					});
				},
				/**
				 * 详情
				 */
				infoFn: function () {
					var _this = this;
					_this.addFlag = false;
					_this.viewTitle = "联系人组查看";
					var selectionsAry = _this.$refs.refTable.selections;
					if (selectionsAry.length != 1) {
						_this.$message({
							message: '请先选择一条记录',
							type: 'warning', duration: 6000
						});
						return;
					}
					_this.switchStatus('DETAIL', false);
					_this.$nextTick(function () {
						_this.$refs.refForm.resetFields();
						yufp.clone(selectionsAry[0], _this.formdata);
					});
				},
				/**
				 * 删除
				 */
				deleteFn: function () {
					var _this = this;
					var selections = _this.$refs.refTable.selections;
					if (selections.length < 1) {
						_this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
						return;
					}
					var len = selections.length,
						arr = [];
					var rolename = '';
					for (var i = 0; i < len; i++) {

						rolename = selections[i].rolename + ',' + rolename;

					}
					_this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning', duration: 6000,
						center: true,
						callback: function (action) {
							if (action === 'confirm') {
								yufp.service.request({
									method: 'GET',
									url: 'api/citiccard/use/warning/contact/delete?rolename=' + rolename,//删除
									// data: rolename,
									callback: function (code, message, response) {
										if (response.code == '0') {
											_this.$refs.refTable.remoteData();
											_this.$message('操作成功');
										} else {
											_this.$message({ message: response.message, type: 'error', showClose: true, duration: 0 });
										}
									}
								});
							}
						}
					});
				},
				/**
				 * 翻页保存数据主键
				 */
				getKey: function (row) {
					return row.targetCode + row.alarmMode;
				}
			}
		});
	};
});

